<template>
  <el-dialog title="外协计划选择"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划编号" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="planList"  @current-change="handleCurrent"  @row-dblclick="handleRowDbClick">
      <el-table-column width="55" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedPlanNo" :label="scope.row.planNo" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="计划编号" align="center" prop="planNo" />
      <el-table-column label="计划名称" align="center" prop="planName" />
<!--      <el-table-column label="预计成本" align="center" prop="totalCost" />-->
      <el-table-column label="需求截止日期" align="center" prop="deadline" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Template from "@/views/print/printtemplate/list.vue";
import BomSelectSingle from "@/components/mdBomSelect/single.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
import PlanLine from "@/views/mes/pro/outsourcingplan/planline.vue";
import {getPlan, listPlan} from "@/api/mes/pro/outsourcingplan";

export default {
  name: "PlanSelector",
  components: {Template, BomSelectSingle, ItemSelect, PlanLine},
  dicts: ['erp_payment_type'],
  props: {

  },
  data() {
    return {
      showFlag: false,
      selectedRows:{lineList:[]},
      oldRow: {},
      lineList: [],
      selectedPlanNo:null,
      autoGenFlag: false,
      optType: 'view',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外协计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNo: null, planName: null,status:1,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planNo: [
          {required: true, message: "计划编号不能为空", trigger: "blur"}
        ], deadline: [
          {required: true, message: "需求截止日期不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询外协计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCurrent(row){
      if(row){
        this.selectedRows = row;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    //双击选中
    handleRowDbClick(row){
      if(row){

        this.selectedPlanNo=row.planNo;
        getPlan(row.planId).then(response => {
          if(response.data.lineList) {
            let list=[];
            response.data.lineList.forEach((obj)=>{
              let data={
                //planlineId
                id:obj.id,
                productId:obj.productId,
                productCode:obj.productCode,
                productName:obj.productName,
                specification:obj.specification,
                unitOfMeasure:obj.unitOfMeasure,
                price:obj.referencePrice,
                status:obj.status,
                productQuantity:obj.productQuantity,
                totalCost:null,
                updateStatus:false,
              }
              list.push(data);
            })
            this.selectedRows = row;
            this.selectedRows.lineList=list;
            this.$emit('onSelected',this.selectedRows);
          }
        });
        this.showFlag = false;
      }
    },
    //确定选中
    confirmSelect(){
      if(this.selectedPlanNo == null || this.selectedPlanNo == 0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      getPlan(this.selectedRows.planId).then(response => {
        if(response.data.lineList) {
          let list=[];
          response.data.lineList.forEach((obj)=>{
            let data={
              //planlineId
              id:obj.id,
              productId:obj.productId,
              productCode:obj.productCode,
              productName:obj.productName,
              specification:obj.specification,
              unitOfMeasure:obj.unitOfMeasure,
              price:obj.referencePrice,
              status:obj.status,
              productQuantity:obj.productQuantity,
              totalCost:null,
              updateStatus:false,
            }
            list.push(data);
          })
          this.selectedRows.lineList=list;
          this.$emit('onSelected',this.selectedRows);
        }
      });
      this.showFlag = false;
    },
  }
}
</script>
